package com.glela.activ.dao.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.glela.activ.dao.OActivContentGiftDao;
import com.glela.activ.dao.OActivGiftProductDao;
import com.glela.activ.mapper.OActivContentGiftMapper;
import com.glela.activ.service.domain.info.OActivInfoContentGiftVo;
import com.glela.activ.service.domain.info.gift.OActivInfoContentGiftProductVo;

@Repository
public class OActivContentGiftDaoImpl implements OActivContentGiftDao {
	//	private static Logger LOGGER = LoggerFactory.getLogger(OActivContentGiftDaoImpl.class);

	@Autowired
	private OActivContentGiftMapper	oActivContentGiftMapper;
	@Autowired
	private OActivGiftProductDao	oActivGiftProductDao;

	@Override
	public OActivInfoContentGiftVo queryByConditionId(Long conditionId) {
		OActivInfoContentGiftVo contentGiftVo = oActivContentGiftMapper.selectByConditionId(conditionId);
		if (contentGiftVo == null) {
			return null;
		}
		contentGiftVo.setGiftProducts(oActivGiftProductDao.queryByContentGiftId(contentGiftVo.getId()));
		if (CollectionUtils.isEmpty(contentGiftVo.getGiftProducts())) {
			//			LOGGER.error("活动{}赠品内容{}未设置赠品{}", contentGiftVo.getActivId(), contentGiftVo.getActivConditionId(), contentGiftVo.getId());
			//			throw new NullPointerException("活动赠品未设置");
			contentGiftVo = null;
		} else {
			for (OActivInfoContentGiftProductVo oActivInfoContentGiftProductVo : contentGiftVo.getGiftProducts()) {
				String imgUrl = oActivGiftProductDao.queryNavigateByGoodsId(oActivInfoContentGiftProductVo.getGoodsId());
				if (StringUtils.isNotBlank(imgUrl)) {
					oActivInfoContentGiftProductVo.setThumbnail(imgUrl);
				}
			}
		}
		return contentGiftVo;
	}

}